package tag;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class Select implements Tag
{
	/*****************************************************************\
	 Static
	\*****************************************************************/
	private static int ID = 0;
	
	private static int getID()
	{
		return ID++;
	}
	
	/*****************************************************************\
	 Attributes
	\*****************************************************************/
	private int id = getID();
	
	private Collection<String> collection = null;
	
	private String name = null;
	
	private Tag tag = null;
	
	private PageContext pageContext = null;
	
	/*****************************************************************\
	 Construction
	\*****************************************************************/
	public Select()
	{
		System.out.println("(" + id + ")Select()");
	}
	
	/*****************************************************************\
	 interface Tag
	\*****************************************************************/
	public int doStartTag()
	{
		System.out.println("(" + id + ")Select.doStartTag()");
		return SKIP_BODY;
	}
	
	public int doEndTag()
	{
		System.out.println("(" + id + ")Select.doEndTag()");
		String body = "<SELECT name=" + name + ">";
		body += "<OPTION>";
		for (int i = 0; i < 74; i++)
			body += "&nbsp;";
		body += "</OPTION>";
		for (String option: collection)
		{
			body += ("<OPTION>" + option + "</OPTION>");
		}
		body += "</SELECT>";
		try
		{
			pageContext.getOut().print(body);
		}
		catch (IOException e)
		{
			System.out.println(e);
		}
		return EVAL_PAGE;
	}
	
	public void setPageContext(PageContext pc)
	{
		System.out.println("(" + id + ")Select.setPageContext(" + pc + ")");
		pageContext = pc;
	}
	
	public Tag getParent()
	{
		System.out.println("(" + id + ")Select.getParent()");
		return tag;
	}
	
	public void setParent(Tag t)
	{
		System.out.println("(" + id + ")Select.setParent(" + t + ")");
		tag = t;
	}
	
	public void release()
	{
		System.out.println("(" + id + ")Select.release()");
	}
	
	/*****************************************************************\
	 CustomTag
	\*****************************************************************/
	public void setCollection(Collection<String> newCollection)
	{
		System.out.println("(" + id + ")Select.setCollection(" + newCollection + ")");
		collection = newCollection;
	}
	
	public void setName(String newName)
	{
		System.out.println("(" + id + ")Select.setName(" + newName + ")");
		name = newName;
	}
	
	/*****************************************************************\
	 finalize
	\*****************************************************************/
	@Override
	protected void finalize() throws Throwable
	{
		System.out.println("(" + id + ")Select.finalize()");
	}
}
